<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include('view.php');

class Controle extends Object{
    var $name = null;

    var $here = null;
    
    var $action = null;
    
    var $params = array();
    
    var $data = array();
    
    var $viewVars = array();
    
    var $base = null;
    
    var $viewPath = array();
    
    var $view = 'View';
    
    var $output = null;
    
    var $passedArgs = array();
    
    var $methods = array();
    
    var $autoRender = true;
    
    var $autoLayout = true;

	function __construct() {
		if ($this->name === null) {
			$r = null;
			if (!preg_match('/(.*)controle/i', get_class($this), $r)) {
				__("Controller::__construct() : Can not get or parse my own class name, exiting.");
				$this->_stop();
			}
			$this->name = $r[1];
		}
        
		if ($this->viewPath == null) {
			//$this->viewPath = Inflector::underscore($this->name);
            $this->viewPath = 'app.view'.DS.strtolower($this->name).DS;
		}
		//$this->modelClass = Inflector::classify($this->name);
		//$this->modelKey = Inflector::underscore($this->modelClass);
		//$this->Component =& new Component();

		$childMethods = get_class_methods($this);
		$parentMethods = get_class_methods('controle');
        
		foreach ($childMethods as $key => $value) {
			$childMethods[$key] = strtolower($value);
		}

		foreach ($parentMethods as $key => $value) {
			$parentMethods[$key] = strtolower($value);
		}
		$this->methods = array_diff($childMethods, $parentMethods);
        //$this->methods = $childMethods;
		parent::__construct();
	}   
    
	function set($one, $two = null) {
		$data = array();

		if (is_array($one)) {
			if (is_array($two)) {
				$data = array_combine($one, $two);
			} else {
				$data = $one;
			}
		} else {
			$data = array($one => $two);
		}
		$this->viewVars = $data + $this->viewVars;
	}
    
function render($action = null, $layout = null){
    $viewClass = $this->view;
    
    $View =& new $viewClass($this);
    
    $this->autoRender = false;
    
    $this->output .= $View->render($action, $layout);

    return $this->output;
}

    function redirect($url, $exit = true){
        $this->autoRender = false;

        if ($url !== null) {
                $this->header("Location: $url");
        }

        if ($exit) {
            $this->_stop();
        }    
    }
    
	function header($status) {
		header($status);
	}    
}
?>
